
/**
 * 弹出框
 * pc端使用
 **/
var ndialog = {
    success: function (message) {
        layer.open({
            content: message,
            icon: 1,
            area: ['500px', '300px'],
            title: '提示'
        });
    },
    error: function (message) {
        layer.open({
            content: message,
            icon: 2,
            area: ['500px', '300px'],
            title: '提示'
        });
    },
    show: function (message, time) {
        if (!time)
            time = 2000;
        layer.msg(message, {time: time});
    },
    alert: function (message, time) {
        if (!time)
            time = 2000;
        layer.open({
            content: message,
            skin: 'msg',
            time: time
        });
    },
    confirm: function (message, sfunc, efunc, btns) {
        if(!btns){
            btns = ['是','否'] //按钮
        }
        layer.confirm(message, {btn: btns, icon: 3, title:'提示'}, function(index){
            sfunc();
            layer.close(index);
        },function(index){
            efunc();
            layer.close(index);
        });
    }
}

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
var najax = {
    post : function(url, data, func, dataType){
        if(!dataType){
            dataType = 'json';
        }
        $.ajax({
            type : 'post',
            url : url,
            data : data,
            dataType : dataType,
            timeout : 5000, //超时时间设置，单位毫秒
            success : function(res){
                func(res);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                ndialog.show('网络失败，请刷新页面后重试');
            },
            complete : function(XMLHttpRequest,status){
                if(status=='timeout') {
                    ndialog.show("超时,请稍后再试");
                }
            }
        });
    },
    get : function(url, data, func, dataType){
        if(!dataType){
            dataType = 'json';
        }
        $.ajax({
            type : 'get',
            url : url,
            data : data,
            dataType : dataType,
            timeout : 30000, //超时时间设置，单位毫秒
            success : function(res){
                func(res);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alter('网络失败，请刷新页面后重试');
            },
            complete : function(XMLHttpRequest,status){
                if(status=='timeout') {
                    alter("超时,请稍后再试");
                }
            }
        });
    }
};


function goUrl(url){
    window.location.href = url;
};

function getUrl(url){
    return url;
}


var nform = {
    submit: function (formId, url, succFunc, errFunc) {
        $.ajax({
            type : "POST",
            url:url,
            data : $('#'+formId).serialize(),// 你的formid
            success: function(data){
                if(data.status == 1){
                    if(data.msg){
                        ndialog.show(data.msg);
                    }else{
                        ndialog.show('操作成功');
                    }
                    if(succFunc){
                        succFunc();
                    }
                }else{
                    if(data.msg){
                        ndialog.show(data.msg);
                    }else{
                        ndialog.show('操作失败');
                    }
                    if(errFunc) errFunc(data);
                }
            }
        },'json');
    },
    delete: function(obj, msg, succFunc, errFunc){
        if(!msg){
            msg = '确认删除';
        }
        if(confirm(msg)){
            $.ajax({
                type : 'post',
                url : $(obj).attr('data-url'),
                dataType : 'json',
                success : function(data){
                    if(data.status==1){
                        if(data.info){
                            ndialog.show(data.info);
                        }else{
                            ndialog.show('删除成功');
                        }
                        if(succFunc){
                            succFunc();
                        } else{
                            window.location.reload();
                        }
                    }else{
                        if(data.info){
                            ndialog.show(data.info);
                        }else{
                            ndialog.show('删除失败');
                        }
                        if(errFunc) errFunc();
                    }
                }
            })
        }
        return false;
    }
}


function ajax_get_more(formId,page,url){
    $.ajax({
        type : "POST",
        url:url+"?p="+page,
        data : $('#'+formId).serialize(),
        success: function(data){
            $("#ajax_return").html('');
            $("#ajax_return").append(data);
        }
    });
}